<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-01-01 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/admin/master.mod.php';
require_once ROOT_PATH.'include/lib/image.func.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
				
		$this->excute();
	}
	
	private function excute()
	{
		ob_start();
		
		switch($this->act)
		{
			case 'add':
				$this->add();
				break;
			case 'do_add':
				$this->doAdd();
				break;
			case 'modify':
				$this->modify();
				break;
			case 'do_modify':
				$this->doModify();
				break;
			case 'do_delete':
				$this->doDelete();
				break;
			case 'units':
				$this->getSeafoodUnits();
				break;
			case 'add_units':
				$this->addSeafoodUnits();
				break;
			case 'del_units':
				$this->deleteSeafoodUnits();
				break;
			case 'history_price':
				$this->historyPrice();
				break;
			case 'delete_history_price':
				$this->deleteHistoryPrice();
				break;
			default:
				$this->main();
				break;
		}
		
		$body = ob_get_clean();
		
		$this->showBody($body);
	}
	
	private function main()
	{
		//DBFunction::getInstance()->setDebug();
		
		$page = $this->request['offset'] + 1;
		
		$name = isset($this->request['name']) ? $this->request['name'] : '';
				
		$query_link = "manage.php?m=seafood";
		$query_link .= $name ? '&name='.urlencode($name) : '';
		$query_link = $query_link;
				
		$result = get_seafood_list($this->request);
		
		$total_record = $result['count'];
		$page_html = page($total_record, $this->request['limit'], $page, $query_link, '10 20 50 100 200 500');
		
		$list = $result['result'];
		
		include template('admin/seafood');
	}
	
	private function add()
	{
		$info = array();
		if(isset($this->request['c']))
		{
			$info = cache_get($this->request['c']);
			cache_delete($this->request['c']);
		}
		
		$info['name'] = isset($info['name']) ? $info['name'] : '';
		$info['units'] = isset($info['units']) ? $info['units'] : '';
		$info['dispath_price'] = isset($info['dispath_price']) ? $info['dispath_price'] : '';
		$info['retail_price'] = isset($info['retail_price']) ? $info['retail_price'] : '';
		//$info['range'] = isset($info['range']) ? $info['range'] : '';
		$info['create_date'] = isset($info['create_date']) ? $info['create_date'] : date('Y-m-d');
		$info['image'] = '';
		
		$result = get_seafood_units_list();
		$units_list = array();
		$units = array();
		foreach($result as $temp)
		{
			$units['name'] = $temp['units'];
			$units['value'] = $temp['id'];
			$units_list[] = $units;
		}
		$units_list = select('units_list', $units_list, '', 'onclick="select_units(\'units_list\')"');
		
		$action = 'do_add';
		$action_name = '添加海鲜';
		include template('admin/seafood_info');		
	}
	
	private function doAdd()
	{
		//DBFunction::getInstance()->setDebug();
		
		$info = $this->_check_seafood_info();
		$info['uid'] = $this->user['id'];
		
		if($_FILES['image']['name'])
		{
			$image_dir = 'data/images/'.rand(1, 99).'/';
			$image_path = move_image_to_path('image', $image_dir);
			if($image_path)
			{
				$info['image'] = $image_path;
			}
		}
		
		$result = add_seafood($info);
		
		if($result)
		{
			$this->messager("添加成功", 'manage.php?m=seafood&a=modify&id='.$result);
		}
		else
		{
			$this->messager("添加失败", 'manage.php?m=seafood&a=add');
		}
	}
	
	private function modify()
	{
		$sid = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$sid)
		{
			$this->messager("请指定ID", 'manage.php?m=seafood');
		}
		
		if(isset($this->request['c']) && ($request = cache_get($this->request['c'])) !== false)
		{
			cache_delete($this->request['c']);
		}
		else
		{
			$info = get_seafood(array('sid'=>$sid));
			
			if(!isset($info['id']) || $info['id'] < 1)
			{
				$this->messager("海鲜不存在", 'manage.php?m=seafood');
			}
		}
		
		$info['create_date'] = date('Y-m-d', $info['create_date']);
		
		$result = get_seafood_units_list();
		$units_list = array();
		$units = array();
		foreach($result as $temp)
		{
			$units['name'] = $temp['units'];
			$units['value'] = $temp['id'];
			$units_list[] = $units;
		}
		$units_list = select('units_list', $units_list, '', 'onclick="select_units(\'units_list\')"');
		
		$action = 'do_modify&id='.$sid;
		$action_name = '编辑海鲜';
		include template('admin/seafood_info');
	}
	
	private function doModify()
	{
		//DBFunction::getInstance()->setDebug();
		
		$sid = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$sid)
		{
			$this->messager("请指定ID", 'manage.php?m=business');
		}
		
		$seafood = get_seafood(array('sid'=>$sid));
		
		if(!isset($seafood['id']) || $seafood['id'] < 1)
		{
			$this->messager("海鲜不存在", 'manage.php?m=business');
		}
		
		$info = $this->_check_seafood_info();
		$info['uid'] = $this->user['id'];
				
		if($_FILES['image']['name'])
		{
			$old_image = $seafood['image'];
			
			if(is_file($old_image))
			{
				delete_image($old_image);
			}
			
			$image_dir = 'data/images/'.rand(1, 99).'/';
			$image_path = move_image_to_path('image', $image_dir);
			if($image_path)
			{
				$info['image'] = $image_path;
			}
		}
		else
		{
			$info['image'] = $seafood['image'];
		}
		
		$result = update_seafood($info);
		
		if($result)
		{
			$this->messager("修改成功", 'manage.php?m=seafood');
		}
		else
		{
			$this->messager("修改失败", 'manage.php?m=seafood');
		}
	}
	
	private function doDelete()
	{
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
				
		if(!$ids)
		{
			$this->messager("请指定ID", 'manage.php?m=seafood');
		}
				
		$result = false;
		if(check_array($ids))
		{
			foreach($ids as $id)
			{
				$result = delete_seafood($id);
			}
		}
		else
		{
			$result = delete_seafood($ids);
		}
		
		if($result)
		{
			$this->messager("删除成功", 'manage.php?m=seafood');
		}
		else
		{
			$this->messager("删除失败", 'manage.php?m=seafood');
		}
	}
	
	private function _check_seafood_info()
	{		
		$info['name'] = isset($this->request['name']) ? $this->request['name'] : '';
		$info['units'] = isset($this->request['units']) ? $this->request['units'] : '';
		$info['dispath_price'] = isset($this->request['dispath_price']) ? (float)$this->request['dispath_price'] : '';
		$info['retail_price'] = isset($this->request['retail_price']) ? (float)$this->request['retail_price'] : '';
		
		//$info['range'] = isset($this->request['range']) ? (float)$this->request['range'] : 0;
		$info['create_date'] = isset($this->request['create_date']) ? strtotime($this->request['create_date']) : time();
						
		$key = md5(implode(',', $info));
		cache_set($key, $info);
		
		$redirect = "manage.php?m=seafood&a=add&c=$key";
				
		$message = '';
		if(!$info['name'] || !$info['units'] || !$info['dispath_price'])
        {
			$this->messager("内容不能为空", $redirect);
		}
		if(strlen($info['name']) > 99)
		{
			$this->messager("名称超长", $redirect);
		}
		if(strlen($info['units']) > 19)
		{
			$this->messager("单位超长", $redirect);
		}
		
		$info['id'] = isset($this->request['id']) ? $this->request['id'] : '';
		$is_exists = check_seafood_exists($info['name'], $info['id']);
		
		if($is_exists)
		{
			$this->messager("海鲜名称 《{$info['name']}》 已存在", 'manage.php?m=seafood');
		}
				
		return $info;
	}
	
	private function getSeafoodUnits()
	{
		//DBFunction::getInstance()->setDebug();
		
		$result = get_seafood_units_list();
		
		$list = $result;
		
		include template('admin/seafood_units');
	}
	
	private function addSeafoodUnits()
	{
		$info['units'] = isset($this->request['units']) ? $this->request['units'] : '';
		
		$result = add_seafood_units($info);
		
		if($result)
		{
			$this->messager("添加成功", 'manage.php?m=seafood&a=units');
		}
		else
		{
			$this->messager("添加失败", 'manage.php?m=seafood&a=units');
		}
	}
	
	private function deleteSeafoodUnits()
	{
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
		
		$result = '';
		if(stripos($ids, ','))
		{
			$ids = explode(',', $ids);
			
			foreach($ids as $id)
			{
				$result = delete_seafood_units($id);
			}
		}
		else
		{
			$result = delete_seafood_units($ids);
		}
		
		if($result)
		{
			$this->messager("删除成功", 'manage.php?m=seafood&a=units');
		}
		else
		{
			$this->messager("删除失败", 'manage.php?m=seafooda=units');
		}
	}
	
	private function historyPrice()
	{
		//DBFunction::getInstance()->setDebug();
		
		$page = $this->request['offset'] + 1;
		
		$id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		//$name = isset($this->request['name']) ? $this->request['name'] : '';
		//$from = isset($this->request['from']) ? $this->request['from'] : '';
		//$to = isset($this->request['to']) ? $this->request['to'] : '';
		$is_gov = isset($this->request['is_gov']) ? $this->request['is_gov'] : '0';
				
		$query_link = "manage.php?m=seafood&a=history_price&id=$id";
		//$query_link .= $name ? 'name='.urlencode($name) : '';
		//$query_link .= $from ? 'from='.$from : '';
		//$query_link .= $to ? 'to='.$to : '';
		$query_link .= $is_gov ? 'is_gov='.$is_gov : '';
				
		$result = get_seafood_history_price($this->request);
		
		$total_record = $result['count'];
		$page_html = page($total_record, $this->request['limit'], $page, $query_link, '10 20 50 100 200 500');
		
		$list = $result['result'];
		
		include template('admin/seafood_history_price');
	}
	
	private function deleteHistoryPrice()
	{
		//DBFunction::getInstance()->setDebug();
		
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
		
		$result = '';
		if(stripos($ids, ','))
		{
			$ids = explode(',', $ids);
			
			foreach($ids as $id)
			{
				$result = delete_seafood_history_price($id);
			}
		}
		else
		{
			$result = delete_seafood_history_price($ids);
		}
		
		if($result)
		{
			$this->messager("删除成功", 'manage.php?m=seafood&a=history_price');
		}
		else
		{
			$this->messager("删除失败", 'manage.php?m=seafooda=history_price');
		}
	}
}